package com.ljw.spring.annotation.test;

import com.ljw.spring.annotation.conf.ConfigOfProfile;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;

/**
 * @FileName IOCTest_Profile
 * @Description TODO
 * @Author ljw
 * @Date 2021/2/3 12:31
 * @Version 1.0
 */
public class IOCTest_Profile {

    @Test
    public void test01(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigOfProfile.class);
        //DataSource类型的bean组件名的数组
        String[] names = context.getBeanNamesForType(DataSource.class);
        for (String name : names) {
            System.out.println(name);
        }

    }

    /**
     * 代码方式激活Profile环境
     */
    @Test
    public void test02(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //1.设置想要激活的环境
        context.getEnvironment().setActiveProfiles("test","dev");
        //2.注册主配置类
        context.register(ConfigOfProfile.class);
        //3.启动刷新器
        context.refresh();
        //DataSource类型的bean组件名的数组
        String[] names = context.getBeanNamesForType(DataSource.class);
        for (String name : names) {
            System.out.println(name);
        }

    }
}
